package com.lcq.demo;

import org.apache.poi.xwpf.usermodel.*;
import org.junit.Test;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName WordPoiTest
 * @Description
 * @Author lichuanqi
 * @Date 2022/3/15 19:04
 * @Version 1.0
 **/

public class WordPoiTest {
    @Test
    public void test() throws IOException {
        File wordOutFile = new File("E:/test/word.docx");
        XWPFHelperTable tableHelper = new XWPFHelperTable();
        XWPFHelper xwpfHelper = new XWPFHelper();
        XWPFDocument document = new XWPFDocument();
        createTitle(document);
        setTitle(document);
        createTable(document, tableHelper);
        XWPFTable table = document.getTableArray(0);
        setTable(table, tableHelper);
        document.write(new FileOutputStream(wordOutFile));
        System.out.printf("文件创建成功");
    }

    private void createTable(XWPFDocument document, XWPFHelperTable tableHelper) {
        XWPFTable table = document.createTable(8, 8);
        tableHelper.setTableWidthAndHAlign(table, "9072", STJc.CENTER);
        List<XWPFTableRow> rows = table.getRows();
        for (XWPFTableRow row : rows) {
            List<XWPFTableCell> tableCells = row.getTableCells();
            for (XWPFTableCell cell : tableCells) {
                XWPFParagraph paragraph = cell.getParagraphArray(0);
                paragraph.setAlignment(ParagraphAlignment.CENTER);
                XWPFRun run = paragraph.createRun();
                run.setFontSize(12);
            }
        }
    }

    private void setTable(XWPFTable table, XWPFHelperTable tableHelper){
        List<XWPFTableRow> rows = table.getRows();
        int i = 0;
        for (XWPFTableRow row : rows) {
            List<XWPFTableCell> tableCells = row.getTableCells();
            for (XWPFTableCell cell : tableCells) {
                XWPFParagraph paragraph = cell.getParagraphArray(0);
                XWPFRun run = paragraph.getRuns().get(0);
                run.setText(""+i);
                i++;
            }
        }
        tableHelper.mergeCellsHorizontal(table, 0, 0, 3);
        tableHelper.mergeCellsVertically(table, 4, 3, 7);
    }

    private void setTitle(XWPFDocument document) {
        List<IBodyElement> bodyElements = document.getBodyElements();
        XWPFParagraph paragraph = document.getParagraphArray(0);
        XWPFRun titleRun = paragraph.getRuns().get(0);
        titleRun.setText("标题");
    }

    private void createTitle(XWPFDocument document) {
        XWPFParagraph paragraph = document.createParagraph();
        paragraph.setAlignment(ParagraphAlignment.CENTER);
        createTitleRun(paragraph);
    }




    private void createTitleRun(XWPFParagraph paragraph) {
        XWPFRun run = paragraph.createRun();

        run.setBold(true);
        run.setColor("000000");
        run.setFontSize(25);
        //run.addBreak();
    }


    @Test
    public void test1() throws IOException {
        ExportWord ew = new ExportWord();
        XWPFDocument document = ew.createXWPFDocument();
        List<List<Object>> list = new ArrayList<List<Object>>();

        List<Object> tempList = new ArrayList<Object>();
        tempList.add("姓名");
        tempList.add("黄xx");
        tempList.add("性别");
        tempList.add("男");
        tempList.add("出生日期");
        tempList.add("2018-10-10");
        list.add(tempList);
        tempList = new ArrayList<Object>();
        tempList.add("身份证号");
        tempList.add("36073xxxxxxxxxxx");
        list.add(tempList);
        tempList = new ArrayList<Object>();
        tempList.add("出生地");
        tempList.add("江西");
        tempList.add("名族");
        tempList.add("汉");
        tempList.add("婚否");
        tempList.add("否");
        list.add(tempList);
        tempList = new ArrayList<Object>();
        tempList.add("既往病史");
        tempList.add("无");
        list.add(tempList);

        Map<String, Object> dataList = new HashMap<String, Object>();
        dataList.put("TITLE", "个人体检表");
        dataList.put("TABLEDATA", list);
        ew.exportCheckWord(dataList, document, "E:/test/expWordTest.docx");
        System.out.println("文档生成成功");
    }
}
